package models

import (
	"encoding/json"
	"github.com/beego/beego/v2/client/orm"
	"go-cmdb/pkg"
	"time"
)

//Dashboard 看板实例模型
type Dashboard struct {
	BModel
	Status      uint8  `orm:"column(status)" json:"status" form:"status"` // 状态(0:被删除啦：1:正常 2:暂停使用)
	Name        string `json:"name" orm:"column(name)" form:"name"`
	UUID        string `json:"uuid" orm:"column(uuid)" form:"uuid"`
	Tmpl        string `json:"tmpl" orm:"column(tmpl)" form:"tmpl"`
	ChartConfig string `json:"chart_config" orm:"column(chart_config)" form:"chart_config"`
}

//MarshalJSON 格式化时间
func (tb Dashboard) MarshalJSON() ([]byte, error) {

	type Alias Dashboard

	return json.Marshal(struct {
		Alias
		CreatedAt string `json:"created_at"`
		UpdatedAt string `json:"updated_at"`
	}{
		Alias:     (Alias)(tb),
		CreatedAt: tb.CreatedAt.Format("2006-01-02 15:04:05"),
		UpdatedAt: tb.UpdatedAt.Format("2006-01-02 15:04:05"),
	})
}

func NewDashboard() *Dashboard {
	return &Dashboard{}
}

//TableName 表名
func (*Dashboard) TableName() string {
	return DashboardTable
}

//ToJson 表名
func (tb *Dashboard) ToJson() string {
	by, _ := json.Marshal(*tb)
	return string(by)
}
func (*Dashboard) GetByPage(pageSize, pageIndex uint64) (lst []Dashboard, count uint64, err error) {

	db := orm.NewOrm()
	offset := (pageIndex - 1) * pageSize
	q := db.QueryTable(NewDashboard()).Filter("status__gt", 0)
	var c int64
	c, err = q.Count()
	count = uint64(c)
	q.OrderBy("-updated_at").Offset(offset).Limit(pageSize).All(&lst)
	if lst == nil {
		lst = make([]Dashboard, 0)
	}
	return

}

type AddDashReq struct {
	Name string `json:"name" form:"name"`
	Tmpl string `json:"tmpl" form:"tmpl"`
}

type ChartConfig struct {
	Cols []ConfigCol `json:"cols"`
}
type ConfigCol struct {
	Charts []ColChart `json:"charts"`
}

type ColChart struct {
	ApiAddr    string `json:"apiAddr"`
	ChartType  string `json:"chartType"`
	ChartTitle string `json:"chartTitle"`
	ChartData  string `json:"chartData"`
}

func (tb *Dashboard) Add(req AddDashReq, uid uint64) (err error) {

	now := time.Now()
	db := orm.NewOrm()
	tb.Name = req.Name
	tb.UUID = pkg.UUID()
	tb.Tmpl = req.Tmpl
	tb.Status = 1
	tb.CreatedAt = now
	tb.UpdatedAt = now
	tb.UpdatedBy = uid
	tb.CreatedBy = uid
	tb.ChartConfig = ""

	_, err = db.Insert(tb)
	return

}

func (tb *Dashboard) Get(id uint64) (m *Dashboard, err error) {
	m = NewDashboard()
	db := orm.NewOrm()
	query := db.QueryTable(tb)
	err = query.Filter("id", id).One(m)
	if m == nil {
		m = nil
	}
	return
}

func (tb *Dashboard) GetByUUID(uuid string) (m *Dashboard, err error) {
	m = NewDashboard()
	db := orm.NewOrm()
	query := db.QueryTable(tb)
	err = query.Filter("uuid", uuid).One(m)
	if m == nil {
		m = nil
	}
	return
}

//type StatusDashReq struct {
//	ID     uint64 `json:"id" form:"id"`
//	Status uint8  `json:"status" form:"status"`
//}

type RemoveDashReq struct {
	ID uint64 `json:"id" form:"id"`
}
type EnableDashReq struct {
	ID     uint64 `json:"id" form:"id"`
	Status uint8  `json:"status" form:"status"`
}

func (tb *Dashboard) CStatus(status uint8, id, uid uint64) (err error) {

	now := time.Now()
	tb.ID = id
	tb.UpdatedAt = now
	tb.UpdatedBy = uid
	tb.Status = status
	db := orm.NewOrm()
	_, err = db.Update(tb, "status", "updated_at", "updated_by")

	return
}

type UpdateDashReq struct {
	ID   uint64 `json:"id" form:"id"`
	Name string `json:"name" form:"name"`
}

func (tb *Dashboard) Update(name string, id, uid uint64) (err error) {

	now := time.Now()
	tb.ID = id
	tb.UpdatedAt = now
	tb.UpdatedBy = uid
	tb.Name = name
	db := orm.NewOrm()
	_, err = db.Update(tb, "name", "updated_at", "updated_by")

	return
}

type SaveDashConfigReq struct {
	ID     uint64 `json:"id" form:"id"`
	Config string `json:"config" form:"config"`
}

func (tb *Dashboard) SaveDashConfig(config string, id, uid uint64) (err error) {

	now := time.Now()
	tb.ID = id
	tb.UpdatedAt = now
	tb.UpdatedBy = uid
	tb.ChartConfig = config
	db := orm.NewOrm()
	_, err = db.Update(tb, "chart_config", "updated_at", "updated_by")

	return
}
